# -*- coding:utf-8 -*-

import os
import django
from celery.schedules import crontab
from django.conf import settings
from celery import Celery

# 创建celery实例对象
app = Celery('ToTest')

# 把celery和django进行组合，识别和加载django的配置文件
# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Main.settings')
django.setup()
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# 加载任务
# 参数必须必须是一个列表，里面的每一个任务都是任务的路径名称
# app.autodiscover_tasks(["任务1","任务2"])

# 或者下面这种
# Load task modules from all registered Django apps.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

app.conf.beat_schedule = {
    # Executes every Monday morning at 7:30 a.m.
    'add-every-monday-morning': {
        # 'task': 'Apps.Demo.tasks.add',
        'task': 'Apps.UITest.tasks.runUiTestTask',
        # 'schedule': crontab(minute='*/3'),
        # 每5秒执行一次,最小设置5秒
        'schedule': 5.0,
        # 'args': (16, 16),
    },
}


"""
celery -A Main worker -l INFO


 celery -A Main flower  --address=127.0.0.1 --port=5566 --persistent=True --db=./flower
 
 
 celery -A Main beat -l INFO --scheduler django_celery_beat.schedulers:DatabaseScheduler

"""